/**
 *
 * @param resultArr
 * @param divId
 * @returns {*}
 */
function getTableDatas(resultArr, divId) {
    if (divId == undefined) {
        divId = "ReportTableDiv";
    }
    var $reportTableDiv = $('#' + divId + '');
    var $trs = $reportTableDiv.find("tr");
    $.each($trs, function (i) {
        var obj = resultArr[i];
        $(this).children("td").each(function () {
            var $div = $(this).children("div");
            obj[$div.attr("key")] = $div.children("input").val();
        });
    });
    return resultArr;
}

/**
 加载表格
 @param
 @return
 */
function loadTable(divId, title, headArr, dataArr) {
    //1.标题
    $("#" + divId).append("<div style='text-align:center;'>" + title + "</div>");
    //2.表头
    var $table = $("<div class='reportTable'></div>").appendTo($("#" + divId));
    var $thead = $("<div class='theadDiv'></div>");
    for (var i = 0; i < headArr.length; i++) {
        var $th = $("<th class='bb-gray'></th>").append(getHeadTh(headArr[i], true));
        $thead.append($th);
    }
    $thead.css("overflow", "hidden");

    $table.append($thead);

    var $lastThDiv = $thead.find("th:last").children("div");

    //3.表数据
    var $tbody = $("<div class='tbodyDiv'></div>");
    for (var row = 0; row < dataArr.length; row++) {
        var obj = dataArr[row];
        var $bodyTr = $("<tr></tr>");
        var columns = $thead.find(".headTextDiv");
        var colum = 0;
        columns.each(function (col, value) {
            var key = $(this).attr("key");
            if (key == undefined) {
                return true;
            }

            var isreadonly = $(this).attr("isreadonly");
            /**
             * 过滤值为0的数据
             */
            if (typeof obj[key] == "number") {
                if (obj[key] == 0 || obj[key == 0.0]) {
                    obj[key] = "";
                }
            }
            var $text = $("<input type='text' row='" + row + "' column='" + colum + "' class='magusText' style='padding:0 5px;' ></input>").width($(this).width()).val(obj[key]);
            if (isreadonly != "false") {
                $text.attr("readOnly", "readOnly");
            }
            var align = getParm($(this).attr("myStyle"), "align");
            if (align != "") {
                $text.css("text-align", align);
            }
            var $textDiv = $("<div key='" + key + "'></div>").width($(this).width()).append($text);
            var $td = $("<td class='bt-gray bl-gray'></td>").append($textDiv);
            if (col == columns.length - 1) {
                $td.addClass("br-gray");
                $td.children("div").width($td.children("div").width() - 2);
            }
            if (row == dataArr.length - 1) {
                $td.addClass("bb-gray");
            }
            $bodyTr.append($td);
            colum++;
        });
        $tbody.append($bodyTr);

    }
    $table.append($tbody);
    $thead.width($thead.width() + MagusUtils.Common.getScrollWidth($tbody));
    $tbody.scroll(function () {
        $thead.scrollLeft($(this).scrollLeft());
    });

    var columns = $thead.find(".headTextDiv");
    columns.each(function () {
        if ($(this).attr("count") != undefined) {
            var count = JSON.parse($(this).attr("count"));
            for (var i = 0; i < count.length; i++) {
                if ($.trim(count[i].rows) == "true") {
                    //行合并
                    var datas = count[i].datas;
                    var result = count[i].result;
                    if (count[i].type == "sum") {//总计
                        rowSum(datas, dataArr, result);
                    } else if (count[i].type == "less") {
                        rowLess(datas, dataArr, result);
                    } else if (count[i].type == "division100") {
                        division1000(datas, dataArr, result);
                    } else if (count[i].type == "division") {
                        division(datas, dataArr, result);
                    }else if(count[i].type == "division100"){
                        division100(datas, dataArr, result)
                    }else if(count[i].type == "division10000"){
                        division10000(datas, dataArr, result)
                    }
                } else if ($.trim(count[i].columns) == "true") {
                    //列合并
                    if (count[i].type == "sum") {//总计
                        var datas = count[i].datas;
                        var result = count[i].result;
                        var column = count[i].column;
                        columnSum(datas, dataArr, column, result);
                    }
                }
            }

        }
    });

}

function columnSum(rows, datas, column, result) {
    for (var row = 0; row < rows.length; row++) {
        var table = $("[row='" + rows[row] + "'][column='" + column + "']");
        table.bind("keyup", function () {
            getColumnTdData(this, rows, result);
        });
    }
}

/**
 * 行总计
 * @param datas
 * @param rows
 * @param result
 */
function rowSum(datas, rows, result) {
    for (var data = 0; data < datas.length; data++) {
        for (var row = 0; row < rows.length; row++) {
            var column = $("[row='" + row + "'][column='" + datas[data] + "']");
            column.bind("keydown", function () {
                getRowTdData(this, datas, result);
            });
        }
    }
}

/**
 * 行减
 * @param datas
 * @param rows
 * @param result
 */
function rowLess(datas, rows, result) {
    for (var data = 0; data < datas.length; data++) {
        for (var row = 0; row < rows.length; row++) {
            var column = $("[row='" + row + "'][column='" + datas[data] + "']");
            column.bind("keydown", function () {
                getRowLessTdData(this, datas, result);
            });
        }
    }
}

/**
 * 行做除法运算后乘以1000
 * @param datas
 * @param rows
 * @param result
 */
function division1000(datas, rows, result) {
    for (var data = 0; data < datas.length; data++) {
        for (var row = 0; row < rows.length; row++) {
            var column = $("[row='" + row + "'][column='" + datas[data] + "']");
            column.bind("keydown", function () {
                getRowDivision1000TdData(this, datas, result);
            });
        }
    }
}/**
 * 行做除法运算后乘以100
 * @param datas
 * @param rows
 * @param result
 */
function division100(datas, rows, result) {
    for (var data = 0; data < datas.length; data++) {
        for (var row = 0; row < rows.length; row++) {
            var column = $("[row='" + row + "'][column='" + datas[data] + "']");
            column.bind("keydown", function () {
                getRowDivision100TdData(this, datas, result);
            });
        }
    }
}}/**
 * 行做除法运算后乘以10000
 * @param datas
 * @param rows
 * @param result
 */
function division10000(datas, rows, result) {
    for (var data = 0; data < datas.length; data++) {
        for (var row = 0; row < rows.length; row++) {
            var column = $("[row='" + row + "'][column='" + datas[data] + "']");
            column.bind("keydown", function () {
                getRowDivision10000TdData(this, datas, result);
            });
        }
    }
}

/**
 * 行做除法运算后乘以1000
 * @param datas
 * @param rows
 * @param result
 */
function division(datas, rows, result) {
    for (var data = 0; data < datas.length; data++) {
        for (var row = 0; row < rows.length; row++) {
            var column = $("[row='" + row + "'][column='" + datas[data] + "']");
            column.bind("keydown", function () {
                getRowDivisionTdData(this, datas, result);
            });
        }
    }
}

/**
 * 列统计和统计的点击事件
 * @param e
 * @param datas
 * @param result
 */
function getColumnTdData(e, datas, result) {
    stopProp();
    setTimeout(
        function () {
            if (!(/^\d+(\.\d+)?$/.test(e.value))) {
                e.value = e.value.replace(/[^\d\.]/g, '');

                var sumData = datas.map(function (item) {
                    return data = $("[row='" + item + "'][column='" + column + "']").val();
                });
                column = $(e).attr("column");
                var sum = sumData.reduce(getSum, 0) == 0 ? "" : sumData.reduce(getSum, 0);
                $("[row='" + result + "'][column='" + column + "']").val(sum).keyup().keydown();
                return false;
            } else {
                column = $(e).attr("column");
                var sumData = datas.map(function (item) {
                    return data = $("[row='" + item + "'][column='" + column + "']").val();
                });
                var sum = sumData.reduce(getSum, 0) == 0 ? "" : sumData.reduce(getSum, 0);
                $("[row='" + result + "'][column='" + column + "']").val(sum).keyup().keydown();
            }
        }, 1);
}

/**
 * 行统计和统计的点击事件
 * @param e
 * @param datas
 * @param result
 */
function getRowTdData(e, datas, result) {
    stopProp();
    setTimeout(
        function () {
            if (!(/^\d+(\.\d+)?$/.test(e.value))) {
                e.value = e.value.replace(/[^\d\.]/g, '');
                var row = $(e).attr("row");
                var sumData = datas.map(function (item) {
                    return data = $("[row='" + row + "'][column='" + item + "']").val();
                });
                var sum = sumData.reduce(getSum, 0) == 0 ? "" : sumData.reduce(getSum, 0);
                $("[row='" + row + "'][column='" + result + "']").val(sum).keyup().keydown();
                return false;
            } else {
                var row = $(e).attr("row");
                var sumData = datas.map(function (item) {
                    return data = $("[row='" + row + "'][column='" + item + "']").val();
                });
                var sum = sumData.reduce(getSum, 0) == 0.0 ? "" : sumData.reduce(getSum, 0);
                $("[row='" + row + "'][column='" + result + "']").val(sum).keyup().keydown();
            }
        }, 1);
}

/**
 * 行统计差统计的点击事件
 * @param e
 * @param datas
 * @param result
 */
function getRowLessTdData(e, datas, result) {
    stopProp();
    setTimeout(
        function () {
            if (!(/^\d+(\.\d+)?$/.test(e.value))) {
                e.value = e.value.replace(/[^\d\.]/g, '');
                var row = $(e).attr("row");
                var sumData = datas.map(function (item) {
                    return data = $("[row='" + row + "'][column='" + item + "']").val();
                });

                var first = parseFloat(sumData.shift() == "" ? 0 : sumData.shift());
                var less = sumData.reduce(getLess, 0) == 0.0 ? "" : sumData.reduce(getLess, 0);

                $("[row='" + row + "'][column='" + result + "']").val((first + less) < 1 ? "" : (first + less)).keyup().keydown();
                return false;
            } else {
                var row = $(e).attr("row");
                var sumData = datas.map(function (item) {
                    return data = $("[row='" + row + "'][column='" + item + "']").val();
                });
                var first = sumData.shift();
                first = parseFloat(first == "" ? 0 : first);
                var less = sumData.reduce(getLess, 0) == 0.0 ? "" : sumData.reduce(getLess, 0);
                less = less == "" ? 0 : less;
                $("[row='" + row + "'][column='" + result + "']").val((first + less) < 1 ? "" : (first + less)).keyup().keydown();
            }
        }, 1);
}

function getRowDivisionTdData(e, datas, result) {
    stopProp();
    setTimeout(
        function () {
            debugger;
            if (!(/^\d+(\.\d+)?$/.test(e.value))) {
                e.value = e.value.replace(/[^\d\.]/g, '');
                var row = $(e).attr("row");
                var sumData = datas.map(function (item) {
                    return data = $("[row='" + row + "'][column='" + item + "']").val();
                });

                var first = parseFloat(sumData.shift() == "" ? 0 : sumData.shift());
                first = !first ? 0 : first;
                var less = parseFloat(sumData);
                less = !less || less == "" ? 0 : less;
                $("[row='" + row + "'][column='" + result + "']").val(!(first / less) || (first / less) == "Infinity" || (first / less) < 1 ? "" : (first / less)).keyup().keydown();
                return false;
            } else {
                var row = $(e).attr("row");
                var sumData = datas.map(function (item) {
                    return data = $("[row='" + row + "'][column='" + item + "']").val();
                });
                var first = sumData.shift();
                first = parseFloat(first == "" ? 0 : first);
                var less = parseFloat(sumData);
                less = !less || less == "" ? 0 : less;
                $("[row='" + row + "'][column='" + result + "']").val(!(first / less) || (first / less) == "Infinity" || (first / less) < 1 ? "" : (first / less)).keyup().keydown();
            }
        }, 1);
}

function getRowDivision1000TdData(e, datas, result) {
    stopProp();
    setTimeout(
        function () {
            debugger;
            if (!(/^\d+(\.\d+)?$/.test(e.value))) {
                e.value = e.value.replace(/[^\d\.]/g, '');
                var row = $(e).attr("row");
                var sumData = datas.map(function (item) {
                    return data = $("[row='" + row + "'][column='" + item + "']").val();
                });

                var first = parseFloat(sumData.shift() == "" ? 0 : sumData.shift());
                first = !first ? 0 : first;
                var less = parseFloat(sumData);
                less = !less || less == "" ? 0 : less;
                $("[row='" + row + "'][column='" + result + "']").val(!(first / less) || (first / less) == "Infinity" || (first / less) * 1000 < 1 ? "" : (first / less) * 1000).keyup().keydown();
                return false;
            } else {
                var row = $(e).attr("row");
                var sumData = datas.map(function (item) {
                    return data = $("[row='" + row + "'][column='" + item + "']").val();
                });
                var first = sumData.shift();
                first = parseFloat(first == "" ? 0 : first);
                var less = parseFloat(sumData);
                less = !less || less == "" ? 0 : less;
                $("[row='" + row + "'][column='" + result + "']").val(!(first / less) || (first / less) == "Infinity" || (first / less) * 1000 < 1 ? "" : (first / less) * 1000).keyup().keydown();
            }
        }, 1);
}
function getRowDivision100TdData(e, datas, result) {
    stopProp();
    setTimeout(
        function () {
            debugger;
            if (!(/^\d+(\.\d+)?$/.test(e.value))) {
                e.value = e.value.replace(/[^\d\.]/g, '');
                var row = $(e).attr("row");
                var sumData = datas.map(function (item) {
                    return data = $("[row='" + row + "'][column='" + item + "']").val();
                });

                var first = parseFloat(sumData.shift() == "" ? 0 : sumData.shift());
                first = !first ? 0 : first;
                var less = parseFloat(sumData);
                less = !less || less == "" ? 0 : less;
                $("[row='" + row + "'][column='" + result + "']").val(!(first / less) || (first / less) == "Infinity" || (first / less) * 100 < 1 ? "" : (first / less) * 100).keyup().keydown();
                return false;
            } else {
                var row = $(e).attr("row");
                var sumData = datas.map(function (item) {
                    return data = $("[row='" + row + "'][column='" + item + "']").val();
                });
                var first = sumData.shift();
                first = parseFloat(first == "" ? 0 : first);
                var less = parseFloat(sumData);
                less = !less || less == "" ? 0 : less;
                $("[row='" + row + "'][column='" + result + "']").val(!(first / less) || (first / less) == "Infinity" || (first / less) * 100 < 1 ? "" : (first / less) * 100).keyup().keydown();
            }
        }, 1);
}function getRowDivision10000TdData(e, datas, result) {
    stopProp();
    setTimeout(
        function () {
            debugger;
            if (!(/^\d+(\.\d+)?$/.test(e.value))) {
                e.value = e.value.replace(/[^\d\.]/g, '');
                var row = $(e).attr("row");
                var sumData = datas.map(function (item) {
                    return data = $("[row='" + row + "'][column='" + item + "']").val();
                });

                var first = parseFloat(sumData.shift() == "" ? 0 : sumData.shift());
                first = !first ? 0 : first;
                var less = parseFloat(sumData);
                less = !less || less == "" ? 0 : less;
                $("[row='" + row + "'][column='" + result + "']").val(!(first / less) || (first / less) == "Infinity" || (first / less) * 10000 < 1 ? "" : (first / less) * 10000).keyup().keydown();
                return false;
            } else {
                var row = $(e).attr("row");
                var sumData = datas.map(function (item) {
                    return data = $("[row='" + row + "'][column='" + item + "']").val();
                });
                var first = sumData.shift();
                first = parseFloat(first == "" ? 0 : first);
                var less = parseFloat(sumData);
                less = !less || less == "" ? 0 : less;
                $("[row='" + row + "'][column='" + result + "']").val(!(first / less) || (first / less) == "Infinity" || (first / less) * 10000 < 1 ? "" : (first / less) * 10000).keyup().keydown();
            }
        }, 1);
}

/**
 * 迭代器
 * @param preValue
 * @param curValue
 * @param index
 * @param array
 * @returns {number}
 */
function getSum(preValue, curValue, index, array) {
    if (curValue == "") {
        curValue = 0;
    }
    return parseFloat(preValue) + parseFloat(curValue);
}

function getLess(preValue, curValue, index, array) {
    if (curValue == "") {
        curValue = 0;
    }
    return parseFloat(preValue) - parseFloat(curValue);
}

function getDivision(preValue, curValue, index, array) {
    if (curValue == "") {
        curValue = 0;
    }
    return parseFloat(preValue) * parseFloat(curValue);
}

function getParm(content, key) {
    if (content == undefined || content == "") {
        return "";
    }
    var content = content.substr(content.indexOf(key) + key.length + 1);
    return content.substr(0, content.indexOf(";"));
}

/**
 加载表格
 @param
 @return
 */
function getHeadTh(obj, isRoot) {
    var $div = $("<div style='float:left;'></div>").width(obj.width);
    var headColor = getParm(obj.style, "headColor");
    if (typeof(obj.key) == "string") {

        if (obj.isreadonly == undefined) {
            obj.isreadonly = "false";
        }
        $div.append(obj.text).attr({
            "key": obj.key,
            "myStyle": obj.style,
            isreadonly: obj.isreadonly
        }).addClass("headTextDiv bt-gray bl-gray").css({"height": obj.height + "px", "line-height": obj.height + "px"});
        if (obj.count != undefined) {
            $div.attr("count", JSON.stringify(obj.count))
        }
        if (headColor != "") {
            $div.css("color", headColor);
        }
    } else if (typeof(obj.key) == "object") {
        var $childDiv = $("<div class='headTextDiv bt-gray bl-gray' style='float:left;' myStyle='" + obj.style + "'>" + obj.text + "</div>").width(obj.width).height(obj.height);
        if (headColor != "") {
            $childDiv.css("color", headColor);
        }
        if (obj.text.indexOf("<br/>") == -1) {
            $childDiv.css("line-height", obj.height + "px");
        } else {
            $childDiv.css({"text-align": "left", "padding": "10px 0 0 10px"});
        }
        $div.append($childDiv);
        for (var i = 0; i < obj.key.length; i++) {
            $div.append(getHeadTh(obj.key[i], false));
        }
    }
    return $div;
}

var downloadFile = function (url, func, dataAjax) {
    var paramString = "?_dc=" + new Date().getTime();
    for (var key in dataAjax.params) {
        paramString += "&" + key + "=" + dataAjax.params[key];
    }
    $("#downloadForm").remove();
    var $form = $("<form id='downloadForm' method='get' action='" + (url + paramString) + "' style='display:none;'>").appendTo($("body"));
    for (var key in dataAjax.data) {
        var $input = $("<input value='" + dataAjax.data[key] + "' name='" + key + "'/>");
        $form.append($input);
    }
    $form[0].submit();
    if (typeof(func) == "string") {
        eval(func);
    } else if (typeof(func) == "function") {
        func();
    }
};
